package com.zwh.controller;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zwh.entity.Banner;
import com.zwh.entity.PageDTO;
import com.zwh.service.BannerService;

@Controller("bannerController")
@RequestMapping("/banner")
public class BannerController {
	@Resource
	private BannerService bannerService;
	@RequestMapping("queryBanner")
	@ResponseBody
	public PageDTO<Banner> queryBanner(Integer page,Integer rows){
		return bannerService.selectBanner(rows,page);
	}
	
	@RequestMapping("updateStatus")
	@ResponseBody
	public String updateStatus(int status,int id){
		bannerService.updateStatus(id, status);
		return "";
	}
	
	@RequestMapping("deleteBanner")
	@ResponseBody
	public String deleteBanner(int id){
		bannerService.deleteBanner(id);
		return "";
	}
	
	//批量删除
	@RequestMapping("deleteBanners")
	@ResponseBody
	public String deleteBanners(Integer[] ids){
		bannerService.deleteBanners(ids);
		return "";
	}
	
	@RequestMapping("addBanner")
	@ResponseBody
	
	public String addBanner(MultipartFile file,HttpSession session,Banner banner) throws IllegalStateException, IOException{
		
		String fileName = file.getOriginalFilename();
		String realPath = session.getServletContext().getRealPath("/image/shouye");
		File destFile = new File(realPath,fileName);
		file.transferTo(destFile);
		
		if(banner.getStatus()==null){
			banner.setStatus(0);
		}
		banner.setImg_path("shouye/"+fileName);
		bannerService.addBanner(banner);
		return "";
	}
}
